package com.woniu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.pojo.Doctor;
import com.woniu.mapper.DoctorMapper;
import com.woniu.service.IDoctorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.QuerySearchDoctorVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author doujiao
 * @since 2022-10-10
 */
@Service
@Slf4j
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements IDoctorService {
    @Resource
    private DoctorMapper doctorMapper;

    /**
     * 分页搜索查找所有医生
     * @param currentPageStr
     * @param pageSizeStr
     * @param searchDoctorVo
     * @return
     */
    public PageResult searchByPage(String currentPageStr, String pageSizeStr, QuerySearchDoctorVo searchDoctorVo) {
        //转Integer
          Integer currentPage = Integer.parseInt(currentPageStr);
          Integer pageSize = Integer.parseInt(pageSizeStr);
        //分页
        IPage<Doctor> page =new Page(currentPage,pageSize);
        PageResult pageResult = new PageResult();
        //传过来的对象不是空的
        if (searchDoctorVo!=null){
            //构造wrap
            QueryWrapper<Doctor> wrapper=new QueryWrapper();
            //机构不为空
            if (searchDoctorVo.getOrganizationId()!=null){
              wrapper.eq("organization_id",searchDoctorVo.getOrganizationId());
            }
            //团队
            if (searchDoctorVo.getTeamId()!=null){
                wrapper.eq("team_id",searchDoctorVo.getOrganizationId());
            }
            //角色
            if (searchDoctorVo.getTeamId()!=null){
                wrapper.eq("role_id",searchDoctorVo.getRoleId());
            }
            //名称
            if (searchDoctorVo.getTeamId()!=null){
                wrapper.eq("name",searchDoctorVo.getName());
            }
            IPage<Doctor> doctorIPage = doctorMapper.selectPage(page, wrapper);
            pageResult.setTotal(doctorIPage.getTotal());
            pageResult.setData(doctorIPage.getRecords());
            return pageResult;
        }
        IPage<Doctor> doctorIPage = doctorMapper.selectPage(page, null);
        log.debug(String.valueOf(doctorIPage.getTotal()));

        pageResult.setTotal(doctorIPage.getTotal());
         pageResult.setData(doctorIPage.getRecords());
         return pageResult;
    }
}
